<!--vue模板-->
<template>
  <div>
    <!--搜索-->
    <div class="toolbar">
      &nbsp; 姓名：
      <el-input
        clearable
        placeholder="请输入员工名称"
        style="width: 200px; margin-right: 10px"
        v-model="filters.name"
      ></el-input>
      &nbsp;&nbsp;&nbsp; 手机号：
      <el-input
        clearable
        placeholder="请输入手机号"
        style="width: 200px; margin-right: 10px"
        v-model="filters.mobile"
      ></el-input>
      &nbsp;&nbsp;&nbsp; 所属部门：
      <el-tree-select
        clearable
        check-strictly
        v-model="filters.daptId"
        :data="treeData"
        :render-after-expand="false"
        style="width: 240px"
        :props="{
          children: 'children',
          label: 'name',
          value: 'id'
        }"
      />
      <div style="float: right; margin-right: 50px">
        <el-button type="primary" @click="init">查询</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </div>
    </div>
    <!--按钮区-->
    <div class="toolbar">
      <el-button type="primary" @click="save()">新增</el-button>
    </div>
    <!--表格数据-->
    <el-table :data="tableData" style="width: 100%" stripe border>
      <el-table-column
        label="序号"
        type="index"
        width="60"
        header-align="center"
        align="center"
      />
      <el-table-column
        prop="name"
        label="姓名"
        width="100"
        header-align="center"
      />
      <el-table-column
        prop="deptName"
        label="所属部门"
        width="180"
        header-align="center"
      />
      <el-table-column
        prop="job"
        label="职务"
        width="100"
        header-align="center"
        align="center"
      />

      <el-table-column
        prop="account"
        label="账号"
        width=""
        header-align="center"
      />
      <el-table-column
        prop="tyoe"
        label="类型"
        width="100"
        header-align="center"
        align="center"
      >
        <template #default="{ row }">
          <span v-if="row.type == 1">普通员工</span>
          <span v-else-if="row.type == 2">内销</span>
          <span v-else>外销</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="performance"
        label="年度业绩"
        header-align="center"
      />
      <el-table-column
        prop="hasFininsh"
        label="已完成业绩"
        header-align="center"
      />
      <el-table-column prop="hasFininsh" label="已完成率" header-align="center">
        <template v-slot="{ row }">
          {{ (row.hasFininsh / row.performance) * 100 }}%
        </template>
      </el-table-column>

      <el-table-column prop="roleId" label="角色" header-align="center" />
      <el-table-column
        prop="hasFininsh"
        label="已完成业绩"
        header-align="center"
      />
      <el-table-column
        prop="status"
        label="状态"
        width="100"
        header-align="center"
        align="center"
      >
        <template #default="{ row }">
          <el-switch
            v-model="row.status"
            :active-value="1"
            :inactive-value="0"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        header-align="center"
        align="center"
        width="180"
      >
        <template #default="{ row }">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            title="编辑"
            @click="save(row)"
          />
          <el-button type="danger" :icon="Delete" circle title="删除" />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div style="height: 60px; line-height: 40px; background-color: #fff">
      <el-pagination
        background
        layout="sizes,prev,pager,next,jumper"
        :total="total"
        style="float: right; margin-right: 20px"
        @current-change="chpg"
        @size-change="chsize"
      />
    </div>

    <!--新增-->
    <SaveOrUpdate
      ref="saveOrDept"
      :visible="isShow"
      @close="isShow = false"
      @refresh="init()"
    />
  </div>
</template>

<!--vue 组件选项定义-->
<script setup>
import { ref, onBeforeMount } from "vue";
import { Delete, Edit } from "@element-plus/icons-vue";
import { queryEmp } from "@/api/employee";
import SaveOrUpdate from "./SaveOrUpdate.vue";
import { queryTree } from "@/api/dept.js";
//表格数据
const tableData = ref([]);
//是否显示新增用户
const isShow = ref(false);
const saveOrDept = ref(null);
//分页参数
const page = ref(1);
const pageSize = ref(10);
const total = ref(0); //总记录数

//搜索条件
const filters = ref({
  name: "",
  mobile: "",
  daptId: ""
});

//部门树
const treeData = ref([]);

//重置
function reset() {
  filters.value = {
    name: "",
    mobile: "",
    deptId: ""
  };
  init();
}

//新增
function save(data) {
  isShow.value = true;
  saveOrDept.value.init(data);
}

//查询部门
function queryDeptTree() {
  queryTree().then(res => {
    if (res.code == 200) {
      treeData.value = res.data;
    }
  });
}

//方法
async function init() {
  let data = {
    page: page.value,
    pageSize: pageSize.value,
    name: filters.value.name,
    mobile: filters.value.mobile,
    deptId: filters.value.daptId
  };
  let res = await queryEmp(data);
  if (res.code == 200) {
    tableData.value = res.data.list;
    page.value = res.data.page;
    pageSize.value = res.data.pageSize;
    total.value = res.data.total;
  }
}

//改变当前页
function chpg(pg) {
  page.value = pg;
  init();
}
//改变每页记录数
function chsize(pgs) {
  pageSize.value = pgs;
  page.value = 1;
  init();
}

//生命周期钩子
onBeforeMount(() => {
  init();
  queryDeptTree();
});
</script>

<!--vue样式 scoped当前样式只适用于当前组件-->
<style scoped>
.toolbar {
  height: 60px;
  line-height: 60px;
  background-color: #fff;
  padding-left: 20px;
}
</style>
